package com.yehaikeji.workflow.service.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.yehaikeji.workflow.bean.user;
import com.yehaikeji.workflow.mapper.userMapper;
import com.yehaikeji.workflow.service.userService;
import com.yehaikeji.workflow.util.calculateMD;

@Service
public class userServiceImpl implements userService {

	@Autowired
	private userMapper userMapper;

	// 新增user
	@Override
	public int insertSelective(String userName, String password, Integer type) {
		// 参数校验
		if(userName.length()<3) {
			throw new RuntimeException("名称不能小于3个字符");
		}
		if (userMapper.selectByuserName(userName) != null) {
			throw new RuntimeException("该名称已被注册");
		}
		if (type <= 0 || type > 3) {
			throw new RuntimeException("类型错误");
		}
		user record = new user();
		String userid = UUID.randomUUID().toString().replaceAll("-", "");
		record.setName(userName);
		record.setUserid(userid);
		record.setCreatedate(new Date());
		record.setSort(1);
		record.setType(type);
		record.setPassword(calculateMD.MD5(password));
		return userMapper.insertSelective(record);
	}

	@Override
	public int updateByuserNmaemodifipas(String userName, String pas1, String pas2) {
		user user = userMapper.selectByuserName(userName);
		if (user != null) {
			
		} else {
			throw new RuntimeException("账号错误");
		}

		Map<String, Object> map = new HashMap<>();
		map.put("name", userName);
		map.put("password",calculateMD.MD5(pas2));
		return userMapper.updateByuserName(map);
	}

	@Override
	public List<user> userlistpagelist(String name, Integer type) {
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("type", type);
		return userMapper.selectfiltratelist(map);
	}

	@Override
	public user userloginservice(user user) {
		user user2 = userMapper.selectByuserName(user.getName());
		if (user2 == null) {
			throw new RuntimeException("账号错误");
		} else if (!calculateMD.MD5(user.getPassword()).equals(user2.getPassword())) {
			throw new RuntimeException("密码错误");
		}
		Map<String, Object> map = new HashMap<>();
		map.put("name", user.getName());
		map.put("ipaddress", user.getIpaddress());
		userMapper.updateByuserName(map);
		user2.setIpaddress(user.getIpaddress());
		return user2;
	}

	@Override
	public int deleteuser(String userid) {
		user user = userMapper.selectByPrimaryKey(userid);
		if (user.getType() != 1) {
			//throw new RuntimeException("用户权限不够");
		} else {
			
		}
		return userMapper.deleteByPrimaryKey(userid);
	}

	@Override
	public String appLogin(String userName, String password) {
		Map<String, String> map = new HashMap<>();
		map.put("name", userName);
		map.put("password", calculateMD.MD5(password));
		List<user> list = userMapper.selectByNameAndPassword(map);
		if(CollectionUtils.isEmpty(list)){
			throw new RuntimeException("用户名或密码不正确");
		}else{
			user user = list.get(0);
			String accessToken = UUID.randomUUID().toString().replaceAll("-", "");
			user.setAccessToken(accessToken);
			userMapper.updateByPrimaryKeySelective(user);
			return accessToken;
		}
	}

	@Override
	public user selectAccessToken(String accessToken) {
		return userMapper.selectByAccessToken(accessToken);
	}

}
